FROM nginx:1.18.0-alpine

ENV TZ="UTC-8" \
        LC_ALL="zh_CN.UTF-8" \
        LANGUAGE="zh_CN:zh:en_US:en" \
        LANG="zh_CN.UTF-8" \
        PHPIZE_DEPS="autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c" \
        PHP_INI_DIR="/usr/local/etc/php" \
        PHP_EXTRA_CONFIGURE_ARGS="--enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --disable-cgi" \
        PHP_CFLAGS="-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" \
        PHP_CPPFLAGS="$PHP_CFLAGS" \
        PHP_LDFLAGS="-Wl,-O1 -pie" \
        GPG_KEYS="42670A7FE4D0441C8E4632349E4FDC074A4EF02D 5A52880781F755608BF815FC910DEB46F53EA312" \
        PHP_VERSION="7.4.16" \
        PHP_URL="https://www.php.net/distributions/php-7.4.16.tar.xz" \
        PHP_ASC_URL="https://www.php.net/distributions/php-7.4.16.tar.xz.asc" \
        PHP_SHA256="1c16cefaf88ded4c92eed6a8a41eb682bb2ef42429deb55f1c4ba159053fb98b"

COPY docker-entrypoint.sh /
COPY docker-php-source /usr/local/bin/
COPY docker-php-ext-* docker-php-entrypoint /usr/local/bin/

RUN	{\
  sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories; \
	apk update; \
  apk add ca-certificates curl tar xz openssl; \
	apk add --virtual .fetch-deps gnupg; \
	apk add --virtual .build-deps $PHPIZE_DEPS \
		argon2-dev coreutils curl-dev libedit-dev libsodium-dev libxml2-dev linux-headers \
		oniguruma-dev openssl-dev openldap-dev sqlite-dev; \
  ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime; \
  echo "Asia/Shanghai" > /etc/timezone; \
  mkdir -p /usr/share/php/smarty3/ && \
    curl -Lqs https://github.com/smarty-php/smarty/archive/v3.1.35.tar.gz | tar xzf - -C /usr/share/php/smarty3/ --strip-components=2; \
	mkdir -p "$PHP_INI_DIR/conf.d"; \
	[ ! -d /var/www/html ] && mkdir -p /var/www/html; \
	chown nginx:nginx /var/www/html && chmod 777 /var/www/html; \
	mkdir -p /usr/src; \
	cd /usr/src; \
	curl -fsSL -o php.tar.xz "$PHP_URL"; \
	[ -n "$PHP_SHA256" ] && echo "$PHP_SHA256 php.tar.xz" | sha256sum -c -; \
	[ -n "$PHP_ASC_URL" ] && { \
    curl -fsSL -o php.tar.xz.asc "$PHP_ASC_URL"; \
		export GNUPGHOME="$(mktemp -d)"; \
		for key in $GPG_KEYS; do \
			gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
		done; \
		gpg --batch --verify php.tar.xz.asc php.tar.xz; \
		gpgconf --kill all; \
		rm -rf "$GNUPGHOME"; }; \
	apk del --no-network .fetch-deps; \
	export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS"; \
	docker-php-source extract; \
	cd /usr/src/php; \
	gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
	./configure \
		--build="$gnuArch" \
		--with-config-file-path="$PHP_INI_DIR" \
		--with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \
		--enable-option-checking=fatal \
		--with-mhash \
		--with-pic \
		--enable-ftp \
		--enable-mbstring \
		--enable-mysqlnd \
		--with-password-argon2 \
		--with-sodium=shared \
		--with-pdo-sqlite=/usr \
		--with-sqlite3=/usr \
		--with-curl \
		--with-libedit \
		--with-openssl \
		--with-zlib \
		--with-pear \
		$(test "$gnuArch" = 's390x-linux-musl' && echo '--without-pcre-jit') \
		${PHP_EXTRA_CONFIGURE_ARGS:-}; \
	make -j "$(nproc)"; \
	find -type f -name '*.a' -delete; \
	make install; \
	find /usr/local/bin /usr/local/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; \
	make clean; \
	cp -v php.ini-* "$PHP_INI_DIR/"; \
  mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"; \
	cd /; \
	docker-php-ext-configure ldap; \
	docker-php-ext-install ldap; \
	docker-php-source delete; \
	runDeps="$(scanelf --needed --nobanner --format '%n#p' --recursive /usr/local | tr ',' '\n' | sort -u \
			| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }')"; \
	apk add $runDeps; \
	apk del --no-network .build-deps; \
	pecl update-channels; \
	rm -rf /tmp/pear ~/.pearrc; \
  docker-php-ext-enable sodium; \
	php --version; \
	cd /usr/local/etc; \
	if [ -d php-fpm.d ]; then \
		sed 's!=NONE/!=!g' php-fpm.conf.default | tee php-fpm.conf > /dev/null; \
		cp php-fpm.d/www.conf.default php-fpm.d/www.conf; \
	else \
		mkdir php-fpm.d; \
		cp php-fpm.conf.default php-fpm.d/www.conf; \
		{ \
			echo '[global]'; \
			echo 'include=etc/php-fpm.d/*.conf'; \
		} | tee php-fpm.conf; \
	fi; \
	{ \
		echo '[global]'; \
		echo 'error_log = /proc/self/fd/2'; \
		echo; echo '; https://github.com/docker-library/php/pull/725#issuecomment-443540114'; echo 'log_limit = 8192'; \
		echo; \
		echo '[www]'; \
		echo '; if we send this to /proc/self/fd/1, it never appears'; \
		echo 'access.log = /proc/self/fd/2'; \
		echo; \
		echo 'clear_env = no'; \
		echo; \
		echo '; Ensure worker stdout and stderr are sent to the main error log.'; \
		echo 'catch_workers_output = yes'; \
		echo 'decorate_workers_output = no'; \
	} | tee php-fpm.d/docker.conf; \
	{ \
		echo '[global]'; \
		echo 'daemonize = no'; \
		echo; \
		echo '[www]'; \
		echo 'listen = 9000'; \
	} | tee php-fpm.d/zz-docker.conf; \
}

ENTRYPOINT ["/docker-entrypoint.sh"]
WORKDIR /var/www/html
STOPSIGNAL SIGQUIT
CMD ["nginx", "-g", "daemon off;"]

